using System;

using Microsoft.Cci;
using Microsoft.FxCop.Sdk;
using Microsoft.FxCop.Sdk.Introspection;

namespace Tyng.Rules
{
	public abstract class DoNotCallMethodBase<T> : MethodInstructionRuleBase<T>
        where T : DoNotCallMethodBase<T>
	{
		private MethodList _doNotCall;

		protected DoNotCallMethodBase(params Method[] doNotCall) : this(new MethodList(doNotCall)) {}

		//TODO: OpCode.Calli? .Net Framework 2.0...
		protected DoNotCallMethodBase(MethodList doNotCall) : base(OpCode.Callvirt, OpCode.Call)
		{
			if(doNotCall == null)
				throw new ArgumentNullException("doNotCall");

			if(doNotCall.Length == 0)
				throw new ArgumentException("At least one method is required", "doNotCall");

			_doNotCall = doNotCall;
		}
		
		protected override void CheckInstruction(Instruction instruction, Method method)
		{
			Method target = instruction.Value as Method;

			if(target == null)
				return;

			//FullName includes params...
			for(int i = 0; i < _doNotCall.Length; i++)
				if(target.FullName == _doNotCall[i].FullName)
					Problems.Add(new Problem(GetResolution(), instruction));
		}


	}
}
